package dao

import "system_go/models"

func AddCommodities(commodities models.Commodity) error {
	err := db.Table("commodity").Select("comid", "comname", "comprice").Create(&commodities).Error
	return err
}

func DelCommodities(id string) error {
	err := db.Table("commodity").Where("comid = ?", id).Delete(models.Commodity{}).Error
	return err
}

func UpdateCommodities(commodities models.Commodity) error {
	err := db.Table("commodity").Where("comid = ?", commodities.Comid).Updates(&commodities).Error
	return err
}

func GetAllCommodities(commoditiesParam map[string]interface{}) (error, []models.Commodity, int64) {
	var commoditiesData []models.Commodity
	page := commoditiesParam["page"].(int)
	pageSize := commoditiesParam["limit"].(int)
	searchName := commoditiesParam["searchName"].(string)
	var total int64
	err := db.Table("commodity").Where("comname like ? ", "%"+searchName+"%").Order("comid ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&commoditiesData).Error
	return err, commoditiesData, total
}
